package com.bocloud.cmp.driver.providers.ali;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyuncs.ecs.model.v20140526.DescribeImagesRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeImagesResponse;
import com.aliyuncs.ecs.model.v20140526.DescribeImagesResponse.Image;
import com.aliyuncs.exceptions.ClientException;
import com.bocloud.cmp.driver.providers.ImageDriver;
import com.bocloud.cmp.model.ImageModel;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.DateTools;
import com.google.common.collect.Lists;

public class AliImageDriver extends AliDriver implements ImageDriver {

	private static Logger logger = LoggerFactory.getLogger(AliImageDriver.class);

	public AliImageDriver() {
		super();
	}

	public AliImageDriver(String accessKey, String secretKey, String regionId) {
		super(accessKey, secretKey, regionId);
	}

	@Override
	public BsmResult list() {
		List<ImageModel> models = Lists.newArrayList();
		DescribeImagesRequest request = new DescribeImagesRequest();
		request.setPageNumber(1);
		request.setPageSize(100);
		try {
			DescribeImagesResponse response = this.getClient().getAcsResponse(request);
			int totalCount = response.getTotalCount();
			List<Image> images = response.getImages();
			for (Image image : images) {
				models.add(this.convert(image));
			}
			int totalPage = totalCount / 100 + 1;
			if (totalPage == 1) {
				return new BsmResult(true, models, "success");
			}
			for (int i = 2; i <= totalPage; i++) {
				request.setPageNumber(i);
				request.setPageSize(100);
				response = this.getClient().getAcsResponse(request);
				images = response.getImages();
				for (Image image : images) {
					models.add(this.convert(image));
				}
			}
			return new BsmResult(true, models, "success");
		} catch (ClientException e) {
			logger.error("list image error:", e);
			return new BsmResult(false, "list image error!");
		}
	}

	@Override
	public BsmResult detail(String imageId) {
		ImageModel model = null;
		BsmResult result = this.list();
		if (result.isSuccess()) {
			@SuppressWarnings("unchecked")
			List<ImageModel> images = (List<ImageModel>) result.getData();
			for (ImageModel image : images) {
				if (image.getImageId().equals(imageId)) {
					model = image;
					break;
				}
			}
		}
		if (null == model) {
			return new BsmResult(false, "get image detail error");
		} else {
			return new BsmResult(true, model, "success");
		}
	}

	public ImageModel convert(Image image) {
		if (null == image) {
			return null;
		}
		ImageModel model = new ImageModel();
		model.setArchitecture(image.getArchitecture().name());
		model.setGmtCreate(DateTools.transfer("yyyy-MM-dd'T'HH:mmZ", image.getCreationTime(), "Z"));
		model.setImageId(image.getImageId());
		model.setKernelVersion(image.getImageVersion());
		model.setName(image.getImageName());
		String[] os = image.getOSName().split(" ");
		if (os.length >= 2) {
			String osName = os[0];
			model.setOsName(osName);
			String osVersion = image.getOSName().substring(osName.length()).trim();
			model.setOsVersion(osVersion);
		} else {
			model.setOsName(image.getOSName());
			model.setOsVersion(image.getOSName());
		}
		model.setOsType(image.getOSType());
		model.setRemark(image.getDescription());
		model.setSize(Double.valueOf(image.getSize()));
		model.setStatus(image.getStatus());
		return model;
	}

	@Override
	public BsmResult remove(String imageId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BsmResult modify(ImageModel image) {
		// TODO Auto-generated method stub
		return null;
	}
}
